//单向链表
function LinkedList(){
    //封装一个内部类
    function Node(data){
        this.data = data
        this.next = null

    }

    //属性
    this.head = null
    this.length = 0

    //追加方法
    LinkedList.prototype.append = function(data){
        //创建新节点
         let newNode = new Node(data)

        if(this.length == 0){//这是第一个节点
            
            this.head = newNode
        }else{

            let current = this.head
            while(current.next){
                current = current.next
            }
            //最后节点指向新节点
            current.next = newNode
        }

        //length+1
        this.length += 1
    }




    //toString方法
    LinkedList.prototype.toString = function(){
        let current = this.head
        let listString = ''
        while(current){
            listString += current.data + " "
            current = current.next
        }
        return listString
    }

    //insert
    LinkedList.prototype.insert = function(position,data){
        //position的越界判断
        if(position<0 || position > this.length )return false

        //2.根据data 创建newNode
        let newNode = new Node(data)

        //3.判断插入的位置是否是第一个
        if(position == 0){
            newNode.next = this.head
            this.head = newNode
        }else{
            let index = 0
            let current = this.head
            let previous = null
            while(index++ < position){
                previous = current
                current =  current.next
            }

            newNode.next = current
            previous.next  = newNode
        }

        //4.length+1
        this.length += 1

        return true
    }

    //get()方法
    LinkedList.prototype.get = function(position){
        //越界判断
        if(position<0||position >= this.length)return null

        //获取对应的data
        let current = this.head
        let index = 0
        while(index++ < position){
            current = current.next
        }
        return current.data
    }

    LinkedList.prototype.update = function(position, newData){
        if(position<0||position >= this.length)return null

        let current = this.head
        let index = 0
        while(index++ < position){
            current = current.next
        }

        current.data = newData

        return true
    }
}

//测试
let list = new LinkedList()

list.append("abc")
list.append("cba")
list.append("nba")

list.insert(0,'aaa')
list.insert(3,'bbb')
list.insert(5,'ccc')


console.log(list);

console.log(list.get(0));

// console.log(list);
